The present invention relates generally to reading from and writing to registers of a processor. Specifically, the present invention is directed to methods and apparatus for managing access to registers that are outside the current stack frame of an application executing on a processor.
A register stack may comprise a number of general registers of a microprocessor, which have been designated for the storage of data required by, and pertaining to, procedures of a stored program being executed by the microprocessor. Specifically, upon execution of a particular procedure, a stack frame associated with the procedure is created within the register stack to store saved registers (containing variables local to a relevant procedure). When a procedure (referred to as the xe2x80x9ccallingxe2x80x9d procedure) calls a further procedure (referred to as the xe2x80x9ccalledxe2x80x9d procedure), the called procedure in turn creates a further stack frame on top of the stack frame for the calling procedure. Accordingly, for a program in which a number of nested procedures are being executed, the register stack may include a corresponding number of stack frames. The stack frame corresponding to the procedure that is currently executing on the processor is referred to as the xe2x80x9ccurrent stack frame.xe2x80x9d A register stack accordingly allows multiple procedures to effectively share a large register file by stacking associated stack frames in both on-chip registers and off-chip memory.
Typical programs exhibit high call/return frequencies with small amplitudes. In other words, typical programs include many procedure calls to procedures that execute for a relatively short amount of time. The overhead associated with the context switches necessitated by procedure calls is relatively high for such programs. A register stack significantly reduces the number of stores (i.e. register saves) at procedures calls and loads (i.e. register restores) at procedure returns that are required for context switching, and thus provides a xe2x80x9ccachexe2x80x9d of information pertinent to nested procedures. As processor operating frequencies continue to increase faster than access times to memory resources (e.g. RAM) decrease, reducing the number of memory accesses performed by a program, as a result of procedure calls and returns, provides a performance advantage. This advantage is becoming increasingly significant in view of the popularity of modular and object-oriented programming, and the use of dynamically linked libraries, which typically result in programs having high call/return frequencies.
As a register stack has a limited size, it will be appreciated that in a deeply nested procedure call sequence, the capacity of the register stack to store additional registers may be exceeded and require what is termed a xe2x80x9cspill.xe2x80x9d A spill involves storing the values of some of the registers of previous stack frames in a backing store in order to create capacity within the register stack to store registers in a new stack frame. Similarly, as the register stack empties on procedure returns, it is desirable to restore (xe2x80x9cfillxe2x80x9d) registers of previous stack frames that have been stored in the backing store to the register stack. Spilling and filling of registers may be handled by a register stack engine. An example of a register stack engine is disclosed in the commonly owned U.S. patent application Ser. No. 09/199,003, entitled xe2x80x9cMethod and Apparatus for Transferring Data Between a Register Stack and a Memory Resource,xe2x80x9d referenced above.
Because the register stack engine may perform spilling and filling operations concurrently with programs executing on the processor, it is necessary to ensure that program operations involving access to registers do not interfere with the spilling and filling operations performed by the register stack engine. In particular, it is necessary to ensure that registers used by programs executing on the processor are not overwritten by the register stack engine and vice versa.
Methods and apparatus are provided for managing access to registers that are outside a current stack frame. In particular, methods and apparatus are provided for preventing a program executing on a processor from writing to registers that are outside the current stack frame. If a program attempts to write to a register that is not in the current stack frame, a fault (such as an illegal operation fault) is generated. Attempts by the program to read from a register that is not in the current stack frame are allowed, although the data returned by such a read attempt has an undefined value.
In one aspect, a method is provided in a processor including a register stack, the register stack including a plurality of register stack frames, each of the register stack frames including zero or more registers, one of the plurality of register stack frames comprising a current register stack frame. The method includes steps of: (A) receiving an instruction whose execution requires writing to a register referenced by the instruction; (B) determining whether the register referenced by the instruction is in the current register stack frame; and (C) executing the instruction only if the register is determined to be in the current register stack frame. The step (B) may include steps of: (B)(1) comparing a register identifier of the register with a size of the current register stack frame; and (B)(2) determining whether the register is in the current register stack frame based on the comparison. The register identifier may be a numerical identifier, and the step (B)(2) may include a step of determining that the register is not in the current register stack frame if the register identifier is greater than or equal to the size of the current register stack frame. The step (C) may include steps of: (C)(1) executing the instruction if the register is determined to be in the current register stack frame; and (C)(2) generating a fault if the register is determined not to be in the current register stack frame. The step (C)(2) may include a step of generating an illegal operation fault if the register is determined not to be in the current register stack frame. The instruction may be one of a plurality of instructions comprising a computer program executing on the processor, and the step (C) may include steps of: (C)(1) executing the instruction if the register is determined to be in the current register stack frame; and (C)(2) halting execution of the program if the register is determined not to be in the current register stack frame.
In another aspect, a method is provided in a processor including a register stack, the register stack including a plurality of register stack frames, each of the register stack frames including zero or more registers, one of the plurality of register stack frames comprising a current register stack frame. The method comprises steps of: (A) receiving an instruction whose execution requires reading from a register identified by a logical register identifier, the logical register identifier identifying a register that is not within the current register stack frame; (B) determining a physical register identifier of a register in the register stack based on the logical register identifier; and (C) executing the instruction using the physical register identifier. The step (C) may include a step of executing the instruction using the physical register identifier without generating a fault. The logical register identifier may indicate a logical register number of the register, and the step (B) may include steps of: (B)(1) determining a difference between the logical register number and a logical register number of a register at a bottom of the current register stack frame; (B)(2) adding the difference to a physical register number of the register at the bottom of the current register stack frame to determine a sum; and (B)(3) generating the physical register number based on the sum. The step (B)(3) may include steps of: (B)(3)(a) performing modulus division of a size of the register stack by the sum to determine a remainder; and (B)(3)(b) assigning the remainder to the physical register number.
In a further aspect, a method is provided in a processor including a register stack, the register stack including a plurality of register stack frames, each of the register stack frames including zero or more registers, one of the plurality of register stack frames comprising a current register stack frame. The method includes steps of: (A) identifying a register to be spilled to a backing store; (B) determining whether the identified register is in the current register stack frame; and (C) spilling the register to the backing store only if the identified register is determined not to be in the current register stack frame. The processor may further include an identifier of a next register in the register stack to spill to the backing store, and the step (B) may include steps of: (B)(1) determining that the identified register is in the current register stack frame when the next register in the register stack to spill to the backing store is in the current stack frame; and (B)(2) determining that the identified register is not in the current register stack frame when the next register in the register stack to spill to the backing store is not in the current stack frame.
In a further aspect, a method is provided in a processor including a register stack, the register stack including a plurality of register stack frames, each of the register stack frames including zero or more registers, one of the plurality of register stack frames comprising a current register stack frame. The method includes steps of: (A) identifying a register to be filled from a backing store; (B) determining whether the identified register is in the current register stack frame; and (C) filling the register from the backing store only if the identified register is determined not to be in the current register stack frame. The processor may further includes an identifier of a next register in the register stack to fill from the backing store, and the step (B) may include steps of: (B)(1) determining that the identified register is in the current register stack frame when the next register in the register stack to fill from the backing store is in the current stack frame; and (B)(2) determining that the identified register is not in the current register stack frame when the next register in the register stack to fill from the backing store is not in the current stack frame.
In other aspects, apparatus and processors are provided for performing the functions described above. Other aspects of the invention include the various combinations of one or more of the foregoing aspects of the invention, as well as the combinations of one or more of the various embodiments thereof as found in the following detailed description or as may be derived therefrom. The foregoing aspects of the invention also have corresponding computer-implemented processes which are also aspects of the present invention. Other embodiments of the present invention may be derived by those of ordinary skill in the art both from the following detailed description of a particular embodiment of the invention and from the description and particular embodiment of a system in accordance with the invention.